/**
 * 
 */
package org.corpus.english.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.corpus.english.model.Word;
import org.corpus.english.model.WordCategoryData;
import org.corpus.english.model.WordCategoryDataHome;
import org.corpus.english.model.WordHome;
import org.corpus.english.model.WordResultConfig;
import org.corpus.english.view.SentenceCategoryTreeNode;
import org.corpus.english.view.WordCategoryTreeNode;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Div;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;


/**
 * @author robbiecheng
 *
 */
public class WordSearchController extends GenericForwardComposer{
	private WordHome wordDao = new WordHome();
	private WordCategoryDataHome wordCateDao = new WordCategoryDataHome();
	private Word current = new Word();
	private WordResultConfig config = new WordResultConfig(true, true, true);
	private List searchResults;
	

	public void doBeforeComposeChildren(Component comp){
		if (Executions.getCurrent().getParameter("id") != null){
			String wordId = Executions.getCurrent().getParameter("id");
			current = wordDao.findById(Integer.parseInt(wordId));
		} else if (Executions.getCurrent().getParameter("word") != null && Executions.getCurrent().getParameter("sent") != null){
			current.setWord(Executions.getCurrent().getParameter("word"));
			current.setSentences(Executions.getCurrent().getParameter("sent"));
		}		
	}
	
	public List getAllWordCategorys(){
		return wordCateDao.findByExample(new WordCategoryData());
	}
	
	public Word getCurrent() {
		return current;
	}
	
	public WordResultConfig getConfig(){
		return config;
	}
	public int getWordCount(){
		return wordDao.findByExample(new Word()).size();
	}
	
	//save sentence category into bean
	public void onSelect$category(SelectEvent evt){
		Set selectedItems = ((Tree)evt.getTarget()).getSelectedItems();
		HashSet categorys = new HashSet(0);
		for (Iterator iterator = selectedItems.iterator(); iterator.hasNext();) {
			categorys.add(((WordCategoryTreeNode)((Treeitem)iterator.next()).getValue()).getValue());
		}
		current.setWordCategoryDatas(categorys);
	}
	//search
	private Div searchResult;
	private Tree category;
	public void onClick$searchBtn(){
		saveSelectedCategories();
		if (searchResults != null)
			searchResults = null;
		searchResults = wordDao.findByQuery(current);
		updateSearchResult();
	}

	/**
	 * 
	 */
	private void saveSelectedCategories() {
		category.getSelectedItems();
	}

	private void updateSearchResult() {
		UpdateManager.updateSearchResult(searchResult, UpdateManager.WORD_SEARCH_RESULT, searchResults, config);
	}
	AnnotateDataBinder binder;
	private void clearSearch(){
		current = new Word();
		binder.loadAll();
	}
	
	//continue to search
	
	public void onClick$continueToSearch(){
		saveSelectedCategories();
		if (searchResults == null)
			searchResults = wordDao.findByQuery(current);
		else
			searchResults.addAll(wordDao.findByQuery(current));
		clearSearch();
		updateSearchResult();
	}
}
